module Hexena.Cmds.EntropyView (run) where

import Data.Array.Unboxed
import Data.List (minimumBy)
import Data.Packed.Vector
import Data.Packed.Matrix
import qualified Data.ByteString.Lazy as BL
import Graphics.Plot
import GSL.Random.Gen
import System.Posix.Time
import System.Random
import System.Environment (getArgs)

import Hexena.Internal.Minimiser
-- import SelfOrganisingMaps
import Hexena.Math.Statistics
import Hexena.Math.Vector

-- histToProbability

calcProbs :: String -> IO (Vector Double)
calcProbs fname = do
  content <- BL.unpack `fmap` BL.readFile fname
  hist <- histToProbability `fmap` calcHist content
  histC <-histToProbability `fmap` (calcHist $ rleBS content)
  return $ fromList $ (elems hist) ++ (elems histC)

main :: IO ()
main = do
  fname <- getArgs
  ds <- mapM calcProbs fname
  print $ "Number of loaded files: " ++ show (length ds)
  let mat = fromRows ds
  -- print mat
  -- print $ getDistanceTotal mat
  -- print $ rows mat
  -- print $ getDistanceTotal $ reorderedCharacteristicMatrix mat [2,1,0] --[0..(rows mat - 1)]
  -- print $ getDistanceTotal $ reorderedCharacteristicMatrix mat [1,2,0] --[0..(rows mat - 1)]
  -- mesh $ mat
  -- mesh $ fromRows $ snd $ minimumBy (\x y -> compare (fst x) (fst y)) $ getAllDistances ds
  g <- newRNG mt19937
  setSeed g . fromInteger =<< randomIO
  print =<< randomiseOList g [0,1,2,3,4,5,6]
  
run :: FilePath -> IO ()
run fn = do
  --ds <- mapM calcProbs fn
  ds <- calcProbs fn
--  print $ "Number of loaded files: " ++ show (length ds)
  g <- newRNG mt19937
  setSeed g . fromInteger =<< randomIO
  print =<< randomiseOList g [0,1,2,3,4,5,6]
